<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input
          v-model="query.blurry"
          clearable
          size="small"
          placeholder="请输入数据源名称"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation show="" :permission="permission" />
    </div>
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="700px"
    >
      <el-form
        ref="form"
        :inline="true"
        :model="form"
        :rules="rules"
        size="small"
        label-width="120px"
      >
        <el-form-item label="数据源名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="数据源编码" prop="dataCode">
          <el-input v-model="form.dataCode" />
        </el-form-item>
        <el-form-item label="数据库类型" prop="dataType">
          <el-select
            v-model="form.dataType"
            clearable
            placeholder="请选择数据库类型"
          >
            <el-option
              v-for="item in dict.data_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数据库账号" prop="dataUsername">
          <el-input v-model="form.dataUsername" />
        </el-form-item>
        <el-form-item label="数据库密码" prop="dataPass">
          <el-input v-model="form.dataPass" />
        </el-form-item>
        <el-form-item label="数据库地址" prop="dataUrl">
          <el-input v-model="form.dataUrl" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="数据库驱动" prop="driverClass">
          <el-input v-model="form.driverClass" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group
            v-model="form.isEnable"
            :disabled="form.id === user.id"
          >
            <el-radio
              v-for="item in dict.user_status"
              :key="item.id"
              :label="item.value"
            >{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >确认</el-button>
      </div>
    </el-dialog>
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%;"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column :selectable="checkboxT" type="selection" width="55" />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="name"
        label="数据源名称"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="dataUsername"
        label="数据库用户"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="dataUrl"
        label="数据源地址"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        prop="driverClass"
        label="数据源驱动"
      />
      <el-table-column label="状态" :align="center" prop="isEnable">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEnable"
            :disabled="user.id === scope.row.id"
            active-color="#409EFF"
            inactive-color="#F56C6C"
            @change="changeEnabled(scope.row, scope.row.isEnable)"
          />
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        prop="createTime"
        width="135"
        label="创建日期"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-permission="['admin', 'datasource:edit', 'datasource:del']"
        label="操作"
        width="115"
        :align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
            :disabled-dle="scope.row.id === user.id"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import crudDataSource from '@/api/system/dataSource';
import CRUD, { presenter, header, form, crud } from '@crud/crud';
import rrOperation from '@crud/RR.operation';
import crudOperation from '@crud/CRUD.operation';
import udOperation from '@crud/UD.operation';
import pagination from '@crud/Pagination';
import { mapGetters } from 'vuex';

const defaultForm = {
  id: null,
  name: null,
  dataType: null,
  dataUsername: null,
  dataCode: null,
  dataPass: null,
  dataUrl: null,
  driverClass: null,
  isEnable: 'true'
};
export default {
  name: 'DataSource',
  components: { crudOperation, rrOperation, udOperation, pagination },
  cruds() {
    return CRUD({
      title: '数据源',
      url: 'rest/api/dataSource',
      crudMethod: { ...crudDataSource }
    });
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  // 数据字典
  dicts: ['user_status', 'data_type'],
  computed: {
    ...mapGetters(['user'])
  },
  data() {
    return {
      permission: {
        add: ['admin', 'datasource:add'],
        edit: ['admin', 'datasource:edit'],
        del: ['admin', 'datasource:del']
      },
      rules: {
        name: [
          { required: true, message: '请输入数据源名称', trigger: 'blur' }
        ],
        dataType: [{ required: true, message: '请选择数据库类型' }],
        dataUsername: [
          { required: true, message: '请输入数据库账号', trigger: 'blur' }
        ],
        dataPass: [
          { required: true, message: '请输入数据库密码', trigger: 'blur' }
        ],
        dataUrl: [
          { required: true, message: '请输入数据库地址', trigger: 'blur' }
        ],
        driverClass: [
          { required: true, message: '请输入数据库驱动', trigger: 'blur' }
        ],
        dataCode: [
          { required: true, message: '数据源编码不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    [CRUD.HOOK.afterAddError](crud) {
      this.afterErrorMethod(crud);
    },
    [CRUD.HOOK.afterEditError](crud) {
      this.afterErrorMethod(crud);
    },
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      form.isEnable = form.isEnable.toString();
    },
    checkboxT(row, rowIndex) {
      return row.id !== this.user.id;
    },
    // 改变状态
    changeEnabled(data, val) {
      this.$confirm(
        '此操作将 "' +
          this.dict.label.user_status[val] +
          '" ' +
          data.name +
          ', 是否继续？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          crudDataSource
            .edit(data)
            .then(res => {
              this.crud.notify(
                this.dict.label.user_status[val] + '成功',
                CRUD.NOTIFICATION_TYPE.SUCCESS
              );
            })
            .catch(() => {
              data.enabled = !data.enabled;
            });
        })
        .catch(() => {
          data.enabled = !data.enabled;
        });
    }
  }
};
</script>

<style scoped></style>
